<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" href="css/header.css" />

	</head>

	<body>
		<header class="mui-bar mui-bar-nav title">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left" style="color: white"></a>
			<a id="link_submit_signature" class="mui-icon mui-icon-right-nav mui-pull-right" style="color: white;font-size: 14px;line-height: 25px;">完成</a>
			<h1 class="mui-title title-color">设置个性签名</h1>
		</header>

		<div class="mui-content">
			<div class="mui-input-row" style="margin: 10px 5px;">
				<textarea id="txt_signature" rows="5" placeholder="修改个性签名" onKeyUp="showLen(this);"></textarea>
				<span id="num" class="mui-pull-right" style="margin-right: 15px;"></span>
			</div>

		</div>

		<script src="js/mui.min.js"></script>
		<script type="text/javascript" src="js/app.js"></script>
		<script type="text/javascript">
			mui.init()

			mui.plusReady(function() {

				var user = app.getUserGlobalInfo();
				var mysignature = user.signature;
				var txt_signature = document.getElementById("txt_signature");
				txt_signature.value = mysignature;
				if(mysignature==null){
					document.getElementById('num').innerHTML = '还可以输入'+24+'个字';
				}else{
					document.getElementById('num').innerHTML = '还可以输入'+(24-mysignature.length)+'个字';
				}
				
				//提交事件，修改昵称
				var link_submit_signature = document.getElementById("link_submit_signature");
				link_submit_signature.addEventListener("tap", function() {
					var newsignature = txt_signature.value;
					if(newsignature.length > 24) {
						app.showToast("个性签名不能超过25位", "error");
						return false;
					}

					plus.nativeUI.showWaiting("请稍后..");
					mui.ajax(app.serverUrl + "/user/setSignature", {
						data: {
							userId: user.id,
							signature: newsignature,
						},
						dataType: 'json', //服务器返回json格式数据
						type: 'post', //HTTP请求类型
						timeout: 10000, //超时时间设置为10秒；
						headers: {
							'Content-Type': 'application/json'
						},
						success: function(data) {
							plus.nativeUI.closeWaiting();

							if(data.success) {
								console.log(JSON.stringify(data));
								//登陆或注册成功后，保存全局用户对象到本地缓存
								var userInfoJson = data.data;
								app.setUserGlobalInfo(userInfoJson);

								// 触发另外一个webview的自定义事件，可以使用 mui.fire()
								var weixin_mysignatureWebview = plus.webview.getWebviewById("weixin-me.html");
								mui.fire(weixin_mysignatureWebview, "refresh");
								app.showToast("修改成功！", "success");
								//页面跳转
								mui.back();
							} else {
								app.showToast("失败", "error");
							}

						}
					});
				});

			});
			showLen(document.getElementById("txt_signature"));

			function showLen(obj) {
				document.getElementById('num').innerHTML = '还可以输入:'+(24-obj.value.length)+'字';
			};
		</script>

	</body>

</html>